package com.capacitydorm.server.web;


import com.capacitydorm.server.common.vo.DormVo;
import com.capacitydorm.server.common.vo.Result;
import com.capacitydorm.server.domain.Dorm;
import com.capacitydorm.server.service.DormService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 蔺思卓
 * 对宿舍进行操作的controller
 */
@RestController
@RequestMapping("/dorm")
public class DormController {
    @Resource
    private DormService dormService;

    /**
     * 添加宿舍
     * @param dorm
     * @return
     */
    @PostMapping
    public ResponseEntity<Result> addDrom(Dorm dorm){

        try {
            List<Dorm> list = dormService.check(dorm);
            if (list.size() != 0){
                return new ResponseEntity<>(new Result(500,"宿舍重复"),HttpStatus.INTERNAL_SERVER_ERROR);
            }
            dormService.addDorm(dorm );
            return new ResponseEntity<>(new Result(201),HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 删除宿舍
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Result> deleteDormById(@PathVariable("id") Integer id ){
        try{
            dormService.delectDormById(id);
            return new ResponseEntity<>(new Result(204),HttpStatus.NO_CONTENT);
        }catch (Exception e){
            e.printStackTrace();
            return  new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据id更改宿舍
     * @param dorm
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    public ResponseEntity<Result> updateDormById(Dorm dorm,@PathVariable("id") Integer id){
        System.out.println(dorm.getFloorId());
        try {
            dorm.setId(id);
            dormService.updateDormById(dorm);
            return new ResponseEntity<>(new Result(200),HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据id查询宿舍(未完全完成)
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<Result> checkDormById(@PathVariable("id") Integer id){
        try {
            Dorm dorm = dormService.checkDormById(id);
            return new ResponseEntity<>( new Result(200,null,dorm),HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 通过宿舍号查宿舍
     * @param word
     * @return
     */
    @GetMapping("/door/{word}")
    public ResponseEntity<Result> checkDormByDoorWords(@PathVariable("word")  String word){
        try {
            Dorm dorm = dormService.checkOneDormByEx(word);
            return new ResponseEntity<>(new Result(200,null,dorm),HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据楼层查宿舍
     * @param id```
     * @return
     */
    @GetMapping("/num/{id}")
    public ResponseEntity<Result> checkDormsByFloorId(@PathVariable("id")  Integer  id){
        try {
            List<Dorm> dorms = dormService.checkDormsByFloorId(id);
            return new ResponseEntity<>(new Result(200,null,dorms),HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 查询所有宿舍
     * @return
     */
    @GetMapping()
    public ResponseEntity<DormVo> findAllDrom(){
        try {
            DormVo dorm = dormService.findAll();
            return new ResponseEntity<>(dorm,HttpStatus.OK);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
